package com.mnsq.subject.controller;

import com.mnsq.common.bean.annotation.UserInfoRequired;
import com.mnsq.common.bean.conf.ThreadLocalInfo;
import com.mnsq.common.bean.dto.LoginInfo;
import com.mnsq.common.bean.entity.subject.Subject;
import com.mnsq.common.controller.BaseController;
import com.mnsq.common.helper.RedisHelper;
import com.mnsq.common.service.BaseService;
import com.mnsq.subject.bean.condition.SubjectCondition;
import com.mnsq.subject.service.MongoDbService;
import com.mnsq.subject.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import matrix.module.common.bean.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 周俭
 */
@Api("主题相关接口（讨论话题、议题、面试题）")
@RestController
@RequestMapping("/api/subject/")
public class SubjectController extends BaseController<Subject, BaseService<Subject, SubjectCondition>, SubjectCondition> {

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private MongoDbService mongoDbService;

    @Autowired
    private RedisHelper redisHelper;

    @PostMapping("/mg/page")
    @UserInfoRequired(required = true)
    public Result page(@RequestBody SubjectCondition subjectCondition) {
        LoginInfo loginInfo = ThreadLocalInfo.LOGIN_INFO.get();
        if (loginInfo.getRoleId() != 1 && loginInfo.getRoleId() != 2) {
            subjectCondition.setCreateUserId(loginInfo.getUserId());
        }
        return super.queryPage(subjectCondition);
    }

    @ApiOperation("详情信息查询")
    @UserInfoRequired
    @GetMapping("/info/detail/{unique}")
    public Result detail(@PathVariable("unique") String unique) {
//        Long userId = ThreadLocalUserId.CURRENT_USER_ID.get();
//        redisHelper.timesRecord(unique, userId, CountRecordType.READ_TIMES.toString());
        return Result.success(subjectService.queryByUnique(unique));
    }

    @Override
    @UserInfoRequired(required = true)
    public Result del(@RequestBody SubjectCondition c) {
        Result del = super.del(c);
        if (del.getBody() instanceof String) {
            mongoDbService.delDoc(del.getBody().toString());
        }
        return del;
    }

    @Override
    @UserInfoRequired(required = true)
    public Result batchDel(@RequestBody SubjectCondition c) {
        String uniques = c.getUnique();
        if (uniques == null) {
            return Result.fail("unique must not be null.");
        }
        String[] str = uniques.split("[,，；。：:]");
        for (String st : str) {
            c.setUnique(st);
            del(c);
        }
        return Result.success(true);
    }

    /**
     * TODO 首页关注动态信息查询
     * 步骤，查询关注用户，查询动态、文章、题目等分页信息，按时间排序，同时查询收藏数量以及评论数量
     *
     * @return
     */
    @PostMapping("/follow/info")
    public Result followInfo() {
        return null;
    }

    /**
     * TODO 首页推荐条目，包括题目，文章
     * TODO 简单推荐算法实现
     *
     * @return
     */
    @PostMapping("/suggest/info")
    @UserInfoRequired
    public Result suggest() {
        return null;
    }

    /**
     * 查询subject作者信息
     *
     * @param subjectCondition
     * @return
     */
    @PostMapping("/user/info")
    public Result userInfo(@RequestBody SubjectCondition subjectCondition) {
        if (StringUtils.isNotBlank(subjectCondition.getUnique())) {
            // TODO 通过查询subject来查询user
        }
        return null;
    }
}
